package com.yupi.springbootinit.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.exception.ThrowUtils;
import com.yupi.springbootinit.mapper.SupportGrindingRecordMapper;
import com.yupi.springbootinit.model.dto.supportgrindingrecord.SupportGrindingRecordQueryRequest;
import com.yupi.springbootinit.model.entity.SupportGrindingRecord;
import com.yupi.springbootinit.model.vo.SupportGrindingRecordVO;
import com.yupi.springbootinit.service.SupportGrindingRecordService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.stream.Collectors;

/**
 * 去支撑打磨记录服务实现
 */
@Service
@Slf4j
public class SupportGrindingRecordServiceImpl extends ServiceImpl<SupportGrindingRecordMapper, SupportGrindingRecord>
        implements SupportGrindingRecordService {

    @Override
    public void validRecord(SupportGrindingRecord record, boolean add) {
        if (record == null) {
            throw new IllegalArgumentException("记录数据为空");
        }

        String processSheetNo = record.getProcessSheetNo();
        String productName = record.getProductName();

        // 创建时，必填参数不能为空
        if (add) {
            ThrowUtils.throwIf(StringUtils.isBlank(processSheetNo), ErrorCode.PARAMS_ERROR, "工艺单号不能为空");
            ThrowUtils.throwIf(StringUtils.isBlank(productName), ErrorCode.PARAMS_ERROR, "产品名称不能为空");
        }

        // 校验参数长度
        if (StringUtils.isNotBlank(processSheetNo) && processSheetNo.length() > 64) {
            throw new IllegalArgumentException("工艺单号过长");
        }
        if (StringUtils.isNotBlank(productName) && productName.length() > 128) {
            throw new IllegalArgumentException("产品名称过长");
        }
    }

    @Override
    public QueryWrapper<SupportGrindingRecord> getQueryWrapper(SupportGrindingRecordQueryRequest queryRequest) {
        QueryWrapper<SupportGrindingRecord> queryWrapper = new QueryWrapper<>();
        if (queryRequest == null) {
            return queryWrapper;
        }

        Long id = queryRequest.getId();
        String processSheetNo = queryRequest.getProcessSheetNo();
        String productName = queryRequest.getProductName();
        String operator = queryRequest.getOperator();
        String sortField = queryRequest.getSortField();
        String sortOrder = queryRequest.getSortOrder();

        // 精确查询
        queryWrapper.eq(id != null, "id", id);

        // 模糊查询
        queryWrapper.like(StringUtils.isNotBlank(processSheetNo), "process_sheet_no", processSheetNo);
        queryWrapper.like(StringUtils.isNotBlank(productName), "product_name", productName);
        queryWrapper.like(StringUtils.isNotBlank(operator), "operator", operator);

        // 日期范围查询
        if (queryRequest.getRecordDateStart() != null) {
            queryWrapper.ge("record_date", queryRequest.getRecordDateStart());
        }
        if (queryRequest.getRecordDateEnd() != null) {
            queryWrapper.le("record_date", queryRequest.getRecordDateEnd());
        }

        // 排序规则
        queryWrapper.orderBy(StringUtils.isNotBlank(sortField),
                sortOrder.equals("ascend"), sortField);

        // 默认按创建时间倒序
        if (StringUtils.isBlank(sortField)) {
            queryWrapper.orderByDesc("create_time");
        }

        return queryWrapper;
    }

    @Override
    public SupportGrindingRecordVO getRecordVO(SupportGrindingRecord record, HttpServletRequest request) {
        if (record == null) {
            return null;
        }
        SupportGrindingRecordVO recordVO = new SupportGrindingRecordVO();
        BeanUtils.copyProperties(record, recordVO);
        return recordVO;
    }

    @Override
    public Page<SupportGrindingRecordVO> getRecordVOPage(Page<SupportGrindingRecord> recordPage, HttpServletRequest request) {
        List<SupportGrindingRecord> recordList = recordPage.getRecords();
        Page<SupportGrindingRecordVO> recordVOPage = new Page<>(recordPage.getCurrent(), recordPage.getSize(), recordPage.getTotal());
        if (CollUtil.isEmpty(recordList)) {
            return recordVOPage;
        }
        // 对象列表转换
        List<SupportGrindingRecordVO> recordVOList = recordList.stream()
                .map(record -> getRecordVO(record, request))
                .collect(Collectors.toList());
        recordVOPage.setRecords(recordVOList);
        return recordVOPage;
    }
}